<?php


namespace app\manager\controller;

use app\common\controller\Common;

class Category extends Common
{
    public function lists(){
        if ($this->request->isAjax()){
            $limit = $this->getPageSize();
            $reCate = \app\common\model\Category::order('sort','asc')->paginate($limit);
            if (count($reCate)>0){
                return $this->listJson($reCate->items(),$reCate->total());
            }
            return $this->errJson('暂无数据');
        }else{
            return view();
        }
    }

    public function addCate(){
      $title =input('title');
      $sort= input('sort');
      if (is_null($title)||is_null($sort)) return $this->errJson('参数错误');
      $ret = \app\common\model\Category::create(['title'=>$title,'sort'=>$sort]);
      if (empty($ret)) return $this->errJson('创建失败');
      return $this->okJson('创建成功');
    }

    public function uploads()
    {
        $cid = input('cid');
        if (is_null($cid)) return $this->errJson('参数错误');
        $ret = $this->upfile('file', [100, 100]);
        if (isset($ret['code']) && $ret['code'] == 2) {
            $reSite = \app\common\model\Category::where('cid', '=', $cid)->find();
            $reSite->ico = $ret['thumb'];
            $res = $reSite->save();
            if ($res) return $this->okJson('上传成功', $ret);
        }
        return $this->errJson('上传失败');
    }

    public function setStatus()
    {//设置状态
        $cid = input('cid');
        if (is_null($cid)) return $this->errJson('参数错误');
        $Category = \app\common\model\Category::get($cid);
        $Category->status == 2 ? $Category->status = -1 : $Category->status = 2;
        $ret = $Category->save();
        if ($ret) return $this->okJson('ok');
        return $this->errJson('err');
    }

}